package com.ohuang.xphook

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PackageInfoFlags
import android.provider.Settings
import android.text.TextUtils
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

internal object AndroidUtil{




    fun readAssetsString(context: Context, fileName: String): String {
        val stringBuilder = StringBuilder()
        try {
            val assetManager = context.assets
            val inputStream: InputStream = assetManager.open(fileName)
            val bufferedReader = BufferedReader(InputStreamReader(inputStream))
            var line: String?=null
            while (bufferedReader.readLine().also { line =if (line==null) it else "\n$it" } != null) {
                stringBuilder.append(line)
            }

            bufferedReader.close()
            inputStream.close()
        } catch (_: IOException) {
        }
        return stringBuilder.toString()
    }



     fun getMetaDataForApplication(context: Context,name:String): String? {

        var data: String? = null
        try {
            val packageManager = context.packageManager
            if (packageManager != null) {
                //注意此处为ApplicationInfo 而不是 ActivityInfo,因为设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                val applicationInfo = packageManager.getApplicationInfo(
                    context.packageName,
                    PackageManager.GET_META_DATA
                )
                if (applicationInfo.metaData != null) {
                    data = applicationInfo.metaData[name].toString()
                }

            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return data
    }



}
